package MyGui;

import java.awt.event.*;
import java.io.*;
import javax.swing.*;

import DistSys.*;

public class MainApplet extends JApplet {
    JButton startButton;
    private MainFrame frame;

    /**Construct the applet*/
    public MainApplet() {
    }

    /**Initialize the applet*/
    public void init() {
        try {
            try{ getAccessLevel( this.getParameter("AccessLevel") );
            }catch(Exception e) { SysConst.iAccessLevel = -1; }
            //System.out.println( "AccessLevel="+SysConst.iAccessLevel);

            WebConst.setWebPath( this.getCodeBase().toString() );

            // Use systems look & feel for the first windows
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

            frame = new MainFrame(getParameter("FileName"));
            frame.setApplet(this);

            startButton = new JButton("Click again to hide iPAD");
            getContentPane().add(startButton);
            startButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    if (frame.isVisible()) {
                        frame.setVisible(false);
                        startButton.setText("Click again to show iPAD");
                    } else {
                        frame.setVisible(true);
                        startButton.setText("Click again to hide iPAD");
                    }
                }
            });
        } catch(Exception e) {
            JOptionPane.showMessageDialog(this, e ); //e.prinsprintStackTrace();
        }
    }

    public void stop() {
        frame.done();
    }

    public String getAppletInfo() {
        return "Applet Information";
    }

    /**Get parameter info*/
    public String[][] getParameterInfo() {
        return null;
    }

    private void getAccessLevel( String sAccessLevel){
        //String sAccessLevel ="00FF00FF00FF00FF00FF00FF00FF00FF0AFF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF";
        //Find the encoded two bytes
        String str = "0x" + sAccessLevel.substring( 2* SysConst.iAceessLevelIndex,
                      2* SysConst.iAceessLevelIndex + 2);

        int iPosition = Integer.decode(str).intValue();
        iPosition %= (sAccessLevel.length() / 2) ;
        //go to the next if the indexed position is the same as iPositon
        if( iPosition == SysConst.iAceessLevelIndex ) { iPosition ++; }

        str = "0x" + sAccessLevel.substring(2*iPosition, 2*iPosition + 2);
        SysConst.iAccessLevel = Integer.decode(str).intValue();
        if( SysConst.iAccessLevel>=128) { SysConst.iAccessLevel -=256;}
    }//end of getAccessLevel(String);
}

